package com.lg.atp.vo.user;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.alibaba.fastjson.annotation.JSONField;
import com.lg.atp.common.Constants;
import com.lg.atp.entity.TblRoleInfo;

public class UserVo  implements UserDetails{



	private String noRole = "MM_ROLE";//未配置URL对应的角色,但需要校验是否修改密码,如果没有修改过密码，则不许访问系统
	private String defaultRole = "DEFAULT_ROLE";//不需要验证是否修改密码的URL角色,修改密码的页面无需限制，由登录限制即可，此处放开
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 720187800927749405L;
	
	private Long id;

	@JSONField(serialize = false)
	private String userPwd;
	
	private String userName;
	
	private Integer accountStatus;
	
	private Long deptId;
	
	
	private List<TblRoleInfo> userRoles;
	

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}



	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}


	public Long getDeptId() {
		return deptId;
	}

	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}


	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	@Override
	public String getPassword() {
		return this.userPwd;
	}

	@Override
	public String getUsername() {
		return this.userName;
	}

	public Integer getAccountStatus() {
		return accountStatus;
	}

	public void setAccountStatus(Integer accountStatus) {
		this.accountStatus = accountStatus;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return this.accountStatus == 1?true:false;
	}


	public List<TblRoleInfo> getUserRoles() {
		return userRoles;
	}

	public void setUserRoles(List<TblRoleInfo> userRoles) {
		this.userRoles = userRoles;
	}


	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<SimpleGrantedAuthority> grantedAuthorities = new ArrayList<SimpleGrantedAuthority>(this.userRoles.size() + 1);
		for (TblRoleInfo role : this.userRoles) {
			grantedAuthorities.add(new SimpleGrantedAuthority(Constants.PREFIX_ROLE+role.getId()));
		}
		//所有URL的访问都需要修改密码之后才能访问
		//grantedAuthorities.add(new SimpleGrantedAuthority(this.noRole));
		//放开修改密码的URL
		grantedAuthorities.add(new SimpleGrantedAuthority(Constants.DEFAULT_ROLE));
		return grantedAuthorities;
	}


}
